package com.example.foodwebbackend.service;

import com.example.foodwebbackend.dto.PageRequest;
import com.example.foodwebbackend.dto.PageResponse;
import com.example.foodwebbackend.entity.FoodCategory;
import com.example.foodwebbackend.mapper.FoodCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FoodCategoryService {

    @Autowired
    private FoodCategoryMapper foodCategoryMapper;

    /**
     * 获取所有分类
     */
    public List<FoodCategory> getAllCategories() {
        return foodCategoryMapper.findAll();
    }

    /**
     * 根据ID获取分类
     */
    public FoodCategory getCategoryById(Integer categoryId) {
        return foodCategoryMapper.findById(categoryId);
    }

    /**
     * 添加分类
     */
    public FoodCategory addCategory(FoodCategory category) {
        category.setCreateTime(LocalDateTime.now());
        foodCategoryMapper.insert(category);
        return category;
    }

    /**
     * 更新分类
     */
    public FoodCategory updateCategory(FoodCategory category) {
        foodCategoryMapper.update(category);
        return foodCategoryMapper.findById(category.getCategoryId());
    }

    /**
     * 删除分类
     */
    public void deleteCategory(Integer categoryId) {
        foodCategoryMapper.deleteById(categoryId);
    }

    /**
     * 根据父级分类ID获取子分类
     */
    public List<FoodCategory> getCategoriesByParentId(Integer parentId) {
        return foodCategoryMapper.findByParentId(parentId);
    }

    /**
     * 分页获取分类列表
     */
    public PageResponse<FoodCategory> getCategoriesWithPagination(PageRequest request) {
        int offset = (request.getPage() - 1) * request.getSize();
        List<FoodCategory> categories;
        int total;

        if (request.getSearch() != null && !request.getSearch().trim().isEmpty()) {
            categories = foodCategoryMapper.findWithPaginationAndSearch(offset, request.getSize(), request.getSearch());
            total = foodCategoryMapper.countByKeyword(request.getSearch());
        } else {
            categories = foodCategoryMapper.findWithPagination(offset, request.getSize());
            total = foodCategoryMapper.count();
        }

        PageResponse<FoodCategory> response = new PageResponse<>();
        response.setData(categories);
        response.setTotal(total);
        response.setPage(request.getPage());
        response.setSize(request.getSize());
        response.setTotalPages((int) Math.ceil((double) total / request.getSize()));

        return response;
    }

    /**
     * 搜索分类
     */
    public List<FoodCategory> searchCategories(String keyword) {
        return foodCategoryMapper.searchByKeyword(keyword);
    }

    /**
     * 批量删除分类
     */
    public void batchDeleteCategories(List<Integer> categoryIds) {
        for (Integer categoryId : categoryIds) {
            foodCategoryMapper.deleteById(categoryId);
        }
    }

    /**
     * 获取分类总数
     */
    public int getCategoryCount() {
        return foodCategoryMapper.count();
    }
} 